package reflect;

import reflect.annotations.AutoRunClass;

import java.io.File;

/**
 * 输出与当前类Test4在同一个包中所有类是否被@AutoRunClass标注
 */

public class Test4 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());
        String packageName = Test4.class.getPackage().getName();
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName(packageName + "." + className);
            //根据类对象判断该类是否被注解@AutoRunClass标注了
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                System.out.println(className + ":被标注了");
            } else {
                System.out.println(className + ":没有被标注");
            }
//           System.out.println(className+(cls.isAnnotationPresent(AutoRunClass.class)?"：被标注了":":没有被标注")); //如果仅是打桩输出写成三目运算符更为简洁，若有其他业务要处理则写成分支
        }
    }
}
